<template>
  <div class="counter-card">
    <h2>计数器示例</h2>
    <p class="description">演示 Vuex 的 state, mutations, actions, getters</p>
    
    <div class="counter-display">
      <div class="count-value" :class="{ even: isEven, odd: !isEven }">
        {{ count }}
      </div>
      <p class="count-info">
        双倍值: {{ doubleCount }} | 
        {{ isEven ? '偶数' : '奇数' }} | 
        操作历史: {{ historyCount }} 次
      </p>
    </div>
    
    <div class="counter-controls">
      <button @click="increment" class="btn btn-primary">+1</button>
      <button @click="decrement" class="btn btn-secondary">-1</button>
      <button @click="incrementBy(5)" class="btn btn-primary">+5</button>
      <button @click="decrementBy(5)" class="btn btn-secondary">-5</button>
      <button @click="incrementAsync" class="btn btn-async" :disabled="loading">
        {{ loading ? '加载中...' : '异步 +1' }}
      </button>
      <button @click="reset" class="btn btn-danger">重置</button>
    </div>
    
    <div class="history-section">
      <h3>操作历史</h3>
      <div class="history-list">
        <div 
          v-for="(item, index) in history.slice(-5).reverse()" 
          :key="index"
          class="history-item"
        >
          <span class="history-type" :class="item.type">
            {{ item.type === 'increment' ? '+' : item.type === 'decrement' ? '-' : '=' }}
          </span>
          <span class="history-value">{{ item.value }}</span>
          <span class="history-time">{{ item.timestamp }}</span>
        </div>
        <p v-if="history.length === 0" class="no-history">暂无操作历史</p>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  name: 'Counter',
  data() {
    return {
      loading: false
    }
  },
  computed: {
    // 使用 mapState 映射 state
    ...mapState('counter', ['count', 'history']),
    
    // 使用 mapGetters 映射 getters
    ...mapGetters('counter', ['doubleCount', 'isEven', 'historyCount'])
  },
  methods: {
    // 使用 mapMutations 映射 mutations
    ...mapMutations('counter', {
      increment: 'INCREMENT',
      decrement: 'DECREMENT',
      reset: 'RESET'
    }),
    
    // 使用 mapActions 映射 actions
    ...mapActions('counter', ['incrementAsync', 'decrementAsync']),
    
    // 自定义方法
    incrementBy(amount) {
      this.$store.commit('counter/INCREMENT', amount)
    },
    
    decrementBy(amount) {
      this.$store.commit('counter/DECREMENT', amount)
    },
    
    // 异步操作示例
    async incrementAsync() {
      this.loading = true
      await this.$store.dispatch('counter/incrementAsync', 1)
      this.loading = false
    }
  }
}
</script>

<style scoped>
.counter-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 24px;
}

h2 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.description {
  color: #7f8c8d;
  margin: 0 0 24px 0;
  font-size: 14px;
}

.counter-display {
  text-align: center;
  margin: 32px 0;
}

.count-value {
  font-size: 64px;
  font-weight: bold;
  margin: 16px 0;
  transition: all 0.3s;
}

.count-value.even {
  color: #3498db;
}

.count-value.odd {
  color: #e74c3c;
}

.count-info {
  color: #7f8c8d;
  font-size: 14px;
  margin: 8px 0;
}

.counter-controls {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 24px 0;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: #3498db;
  color: white;
}

.btn-secondary {
  background: #95a5a6;
  color: white;
}

.btn-async {
  background: #9b59b6;
  color: white;
}

.btn-danger {
  background: #e74c3c;
  color: white;
}

.history-section {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #ecf0f1;
}

.history-section h3 {
  margin: 0 0 16px 0;
  color: #2c3e50;
  font-size: 18px;
}

.history-list {
  max-height: 200px;
  overflow-y: auto;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  margin: 4px 0;
  background: #f8f9fa;
  border-radius: 4px;
}

.history-type {
  font-weight: bold;
  width: 24px;
  text-align: center;
}

.history-type.increment {
  color: #27ae60;
}

.history-type.decrement {
  color: #e74c3c;
}

.history-type.reset,
.history-type.set {
  color: #f39c12;
}

.history-value {
  font-weight: 500;
  color: #2c3e50;
}

.history-time {
  margin-left: auto;
  color: #7f8c8d;
  font-size: 12px;
}

.no-history {
  text-align: center;
  color: #bdc3c7;
  padding: 16px;
}
</style>

